#
# AS - C++ Application server
#
PROJECT(CAS_MEMCACHED)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)

# ################################################################################
# Defaults
#
OPTION(DEBUG_MODE           "Enable debug output [default: OFF]" ON)
OPTION(ENABLE_OPTIMIZATION  "Enable optimization [default: OFF]" OFF)
OPTION(SKIP_RELINK_RPATH    "Skip relinking and full RPATH for the install tree [default: OFF]" OFF)

# ################################################################################
# Version
#
SET(CAS_MEMCACHED_VERSION_MAJOR 3)
SET(CAS_MEMCACHED_VERSION_MINOR 6)
SET(CAS_MEMCACHED_VERSION_PATCH 2)

SET(CAS_MEMCACHED_VERSION "${CAS_MEMCACHED_VERSION_MAJOR}.${CAS_MEMCACHED_VERSION_MINOR}.${CAS_MEMCACHED_VERSION_PATCH}")
# ################################################################################
# Macroses
#

# Standard CMake macroses
INCLUDE(CheckIncludeFiles)
INCLUDE(CheckCXXSourceCompiles)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckLibraryExists)
INCLUDE(FindZLIB)

# User-defined macroses
INCLUDE(cmake/ConfigureLibrary.cmake)
INCLUDE(cmake/ConfigurePackage.cmake)
INCLUDE(cmake/RenameModule.cmake)

# ################################################################################
# Set system-specific data
#
MESSAGE(STATUS "#### System name is: ${CMAKE_SYSTEM_NAME}")

# ################################################################################
# Setup libraries & headers
#

#
# Configure CTPP2 package
#
CONFIGURE_PACKAGE(ctpp2-config ctpp2 CTPP2)

#
# Configure CAS package
#
CONFIGURE_PACKAGE(cas-config cas CAS)

INCLUDE_DIRECTORIES(include)

# ################################################################################
# Platform-specific options
#
SET (RT_LIBRARY)
IF("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")
    MESSAGE(STATUS "Building for FreeBSD...")

ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")

IF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
    MESSAGE(STATUS "Building for Linux...")

    SET(CMAKE_PLATFORM_BUILD_FLAGS "-D_LARGEFILE64_SOURCE")

    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64")

ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")

IF("${CMAKE_SYSTEM_NAME}" MATCHES "SunOS")
    MESSAGE(STATUS "Building for SunOS...")

    FIND_LIBRARY(RT_LIBRARY NAMES socket PATHS /lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the socket library can be found")

    FIND_LIBRARY(RT_LIBRARY NAMES nsl PATHS /lib
                                            /usr/lib
                                            /usr/local/lib
                 DOC "Path where the nsl library can be found")

ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "SunOS")

IF("${CMAKE_COMPILER_IS_GNUCXX}" MATCHES "1")

    SET(CMAKE_CXX_WARN_FLAGS   "-Werror -Wall -pedantic -Wno-long-long")
    IF(DEBUG_MODE MATCHES "ON")
        MESSAGE(STATUS "#### Debug mode ON")
        SET(CMAKE_CXX_DEFINES      "-g -D_DEBUG")
        SET(CMAKE_CXX_OPTIMIZATION "-O0")
    ELSE (DEBUG_MODE MATCHES "OFF")
        MESSAGE(STATUS "#### Debug mode OFF")
        SET(CMAKE_CXX_DEFINES      "")
        SET(CMAKE_CXX_ARCH_FLAGS   "")
    ENDIF(DEBUG_MODE MATCHES "ON")

    IF(ENABLE_OPTIMIZATION MATCHES "ON")
        MESSAGE(STATUS "#### Optimization ON")
        SET(CMAKE_CXX_OPTIMIZATION "-O3 -fomit-frame-pointer -unroll-loops")
    ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")

    SET(CMAKE_CXX_FLAGS       "${CXX_FLAGS} ${CMAKE_CXX_WARN_FLAGS} ${CMAKE_CXX_DEFINES} ${CMAKE_CXX_ARCH_FLAGS} ${CMAKE_CXX_OPTIMIZATION}")

    IF("${SKIP_RELINK_RPATH}" MATCHES "ON")
        SET(CMAKE_SKIP_RPATH  TRUE)
        MESSAGE(STATUS "#### Building for Linux...")
        # skip the full RPATH for the build tree
        SET(CMAKE_SKIP_BUILD_RPATH  TRUE)
        # when building, use the install RPATH already
        # (so it doesn't need to relink when installing)
        SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
        # the RPATH to be used when installing
        SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
        # add the automatically determined parts of the RPATH
        # which point to directories outside the build tree to the install RPATH
        SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    ENDIF("${SKIP_RELINK_RPATH}" MATCHES "ON")
ENDIF("${CMAKE_COMPILER_IS_GNUCXX}" MATCHES "1")

# ################################################################################
# Lib binary
#
ADD_LIBRARY(casmemcached SHARED src/ASMemcachedObject.cpp
                                src/ASMemcached.cpp)
SET_TARGET_PROPERTIES(casmemcached PROPERTIES OUTPUT_NAME casmemcached)
SET_TARGET_PROPERTIES(casmemcached PROPERTIES LINKER_LANGUAGE CXX)
SET_TARGET_PROPERTIES(casmemcached PROPERTIES VERSION ${CAS_MEMCACHED_VERSION} SOVERSION ${CAS_MEMCACHED_VERSION_MAJOR}.${CAS_MEMCACHED_VERSION_MINOR})
TARGET_LINK_LIBRARIES(casmemcached ${HAVE_ctpp2}
                                   ${HAVE_cas}
                                   ${ZLIB_LIBRARIES})

ADD_LIBRARY(mod_memcached_module MODULE src/ASMemcachedConnector.cpp)
TARGET_LINK_LIBRARIES(mod_memcached_module casmemcached)
RENAME_MODULE(mod_memcached_module mod_memcached_module)

ADD_LIBRARY(mod_memcached_test MODULE src/ASMemcachedTest.cpp)
TARGET_LINK_LIBRARIES(mod_memcached_test casmemcached)
RENAME_MODULE(mod_memcached_test mod_memcached_test)

ADD_EXECUTABLE(test-memcached tests/MemcachedTest.cpp)
TARGET_LINK_LIBRARIES(test-memcached casmemcached)

ADD_EXECUTABLE(test-hash tests/ConsistentHash.cpp)
TARGET_LINK_LIBRARIES(test-hash casmemcached)

# ################################################################################
# Install library headers
#
INSTALL(FILES include/ASMemcachedObject.hpp
              include/ASMemcached.hpp
              include/ASConsistentHash.hpp
        DESTINATION include/cas/modules
        PERMISSIONS OWNER_READ OWNER_WRITE
                    GROUP_READ
                    WORLD_READ)

# ################################################################################
# Install library
#
INSTALL(TARGETS casmemcached DESTINATION lib)

# ################################################################################
# Install modules
#
INSTALL(FILES mod_memcached_module.so
              mod_memcached_test.so
        DESTINATION libexec/cas
        PERMISSIONS OWNER_READ OWNER_WRITE
                    GROUP_READ
                    WORLD_READ)

# End.
